package demo05173;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Demo2 {

        public static void menu() {
            System.out.println("员工管理系统");
            System.out.println("0.退出系统");
            System.out.println("1.添加员工");
            System.out.println("2.查询所有员工");
            System.out.println("3.根据员工姓名查询");
            System.out.println("4.删除员工");
            System.out.println("5.根据员工编号查询");
            System.out.println("6.根据员工编号修改年龄");
            System.out.println("选择操作");
        }

        public static void main(String[] args) {
            TestMap testMap = new TestMap();
          Map<String, Emp> map = new HashMap<>();

            boolean flag = true;
            while (flag) {
                menu();
               Scanner scanner = new Scanner(System.in);
                int n = scanner.nextInt();
                switch (n) {
                    case 0:
                        flag = false;
                        System.out.println("系统已退出");
                        break;
                    case 1:
                        System.out.println("输入员工信息");
                        System.out.println("1.输入员工编号");
                        String empNumber = scanner.next();
                        // 检查编号是否已存在
                        if (map.containsKey(empNumber)) {
                            System.out.println("该员工编号已存在！");
                            break;
                        }
                        System.out.println("2.输入员工姓名");
                        String empName = scanner.next();
                        System.out.println("3.输入员工年龄");
                        int empAge = scanner.nextInt();

                        Emp addEmp = new Emp(empNumber, empName, empAge);
                        testMap.addEmp(map, addEmp);
                        System.out.println("添加成功：" + addEmp);
                        break;
                    case 2:
                        System.out.println("所有员工信息：");
                        testMap.findAllEmp(map);
                        break;
                    case 3:
                        System.out.println("输入要查询的员工姓名：");
                        Emp foundByName = testMap.findEmpName(map, scanner.next());
                        System.out.println(foundByName != null ? "查询结果：" + foundByName : "未找到该员工");
                        break;
                    case 4:
                        System.out.println("输入要删除的员工编号：");
                        String delNumber = scanner.next();
                        if (!map.containsKey(delNumber)) {
                            System.out.println("该员工编号不存在！");
                            break;
                        }
                        testMap.deleteEmpNumber(map, delNumber);
                        System.out.println("删除成功");
                        break;
                    case 5:
                        System.out.println("输入要查询的员工编号：");
                        String searchNumber = scanner.next();
                        Emp foundByNumber = testMap.findEmpNumber(map, searchNumber);
                        System.out.println(foundByNumber != null ? "查询结果：" + foundByNumber : "未找到该员工");
                        break;
                    case 6:
                        System.out.println("输入要修改的员工编号：");
                        String updateNumber = scanner.next();
                        if (!map.containsKey(updateNumber)) {
                            System.out.println("该员工编号不存在！");
                            break;
                        }
                        System.out.println("输入新的年龄：");
                        int newAge = scanner.nextInt();
                        Emp empToUpdate = map.get(updateNumber);
                        empToUpdate.setAge(newAge);
                        testMap.updateEmpAge(map, empToUpdate);
                        System.out.println("修改成功：" + empToUpdate);
                        break;
                    default:
                        System.out.println("无效的选择，请重新输入！");
                }
            }
        }
    }
